Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.6, n = 406)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.03282 13.02871 13.02464 13.02063 13.01665 13.01273 13.00885 13.00502
## [9] 13.00123 12.99749 12.99380 12.99015 12.98654 12.98298 12.97947 12.97600
## [17] 12.97257 12.96919 12.96586 12.96256 12.95932 12.95611 12.95296 12.94984
## [25] 12.94677 12.94374 12.94076 12.93782 12.93493 12.93207 12.92926 12.92650
## [33] 12.92377 12.92109 12.91845 12.91586 12.91331 12.91082 12.90839 12.90601
## [41] 12.90368 12.90141 12.89920 12.89704 12.89494 12.89288 12.89089 12.88894
## [49] 12.88705 12.88522 12.88343 12.88170 12.88002 12.87839 12.87682 12.87529
## [57] 12.87382 12.87240 12.87103 12.86970 12.86843 12.86721 12.86604 12.86492
## [65] 12.86385 12.86283 12.86185 12.86093 12.86005 12.85922 12.85844 12.85770
## [73] 12.85701 12.85637 12.85578 12.85523 12.85473 12.85428 12.85389 12.85360
## [81] 12.85339 12.85327 12.85323 12.85328 12.85340 12.85360 12.85387 12.85421
## [89] 12.85463 12.85511 12.85565 12.85625 12.85692 12.85764 12.85841 12.85924
## [97] 12.86011 12.86104 12.86200 12.86301 12.86406 12.86514 12.86626 12.86742
## [105] 12.86860 12.86981 12.87104 12.87230 12.87357 12.87486 12.87617 12.87749
## [113] 12.87882 12.88016 12.88151 12.88285 12.88420 12.88554 12.88688 12.88822
## [121] 12.88986 12.89212 12.89493 12.89826 12.90205 12.90626 12.91084 12.91575
## [129] 12.92093 12.92635 12.93195 12.93768 12.94350 12.94936 12.95522 12.96103
## [137] 12.96674 12.97230 12.97766 12.98279 12.98763 12.99213 12.99625 12.99994
## [145] 13.00316 13.00585 13.00798 13.01082 13.01559 13.02206 13.03004 13.03933
## [153] 13.04971 13.06099 13.07296 13.08541 13.09815 13.11096 13.12365 13.13600
## [161] 13.14782 13.15890 13.16904 13.17803 13.18567 13.19175 13.19607 13.19842
## [169] 13.20002 13.20218 13.20483 13.20791 13.21139 13.21518 13.21925 13.22353
## [177] 13.22796 13.23250 13.23707 13.24163 13.24613 13.25049 13.25467 13.25861
## [185] 13.26225 13.26553 13.26841 13.27082 13.27270 13.27401 13.27467 13.27464
## [193] 13.27386 13.27228 13.26982 13.26645 13.26210 13.25672 13.25012 13.24223
## [201] 13.23309 13.22276 13.21129 13.19875 13.18518 13.17065 13.15521 13.13891
## [209] 13.12182 13.10398 13.08546 13.06631 13.04659 13.02635 13.00564 12.98454
## [217] 12.96308 12.94133 12.91935 12.89718 12.87489 12.85253 12.83016 12.80783
## [225] 12.78561 12.76354 12.74168 12.72009 12.69882 12.67794 12.65749 12.63753
## [233] 12.61813 12.59932 12.58118 12.56376 12.54711 12.53129 12.51528 12.49809
## [241] 12.47986 12.46071 12.44077 12.42016 12.39900 12.37743 12.35557 12.33355
## [249] 12.31148 12.28950 12.26774 12.24631 12.22534 12.20496 12.18530 12.16648
## [257] 12.14862 12.13186 12.11631 12.10185 12.08820 12.07530 12.06307 12.05146
## [265] 12.04038 12.02977 12.01956 12.00968 12.00005 11.99062 11.98131 11.97205
## [273] 11.96277 11.95340 11.94387 11.93412 11.92407 11.91365 11.90280 11.89144
## [281] 11.88035 11.87032 11.86124 11.85302 11.84557 11.83880 11.83261 11.82691
## [289] 11.82162 11.81662 11.81185 11.80719 11.80256 11.79786 11.79301 11.78791
## [297] 11.78247 11.77659 11.77018 11.76315 11.75541 11.74653 11.73628 11.72482
## [305] 11.71234 11.69900 11.68499 11.67048 11.65564 11.64065 11.62568 11.61091
## [313] 11.59652 11.58268 11.56956 11.55735 11.54621 11.53632 11.52785 11.52099
## [321] 11.51591 11.51278 11.51055 11.50811 11.50552 11.50283 11.50012 11.49744
## [329] 11.49485 11.49242 11.49021 11.48828 11.48669 11.48550 11.48478 11.48459
## [337] 11.48498 11.48603 11.48779 11.49033 11.49370 11.49797 11.50320 11.50899
## [345] 11.51494 11.52106 11.52738 11.53393 11.54074 11.54783 11.55523 11.56296
## [353] 11.57106 11.57955 11.58845 11.59780 11.60761 11.61793 11.62877 11.64015
## [361] 11.65212 11.66469 11.67789 11.69175 11.70618 11.72108 11.73645 11.75230
## [369] 11.76861 11.78539 11.80264 11.82036 11.83855 11.85720 11.87633 11.89592
## [377] 11.91598 11.93651 11.95750 11.97896 12.00089 12.02328 12.04614 12.06946
## [385] 12.09325 12.11751 12.14222 12.16741 12.19305 12.21917 12.24574 12.27278
## [393] 12.30028 12.32824 12.35667 12.38555 12.41490 12.44472 12.47499 12.50572
## [401] 12.53692 12.56857 12.60068 12.63326 12.66629 12.69979
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.6, n = 406)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.55298 12.55115 12.54936 12.54761 12.54590 12.54423 12.54261 12.54102
## [9] 12.53949 12.53800 12.53656 12.53517 12.53383 12.53255 12.53132 12.53014
## [17] 12.52902 12.52796 12.52696 12.52602 12.52515 12.52434 12.52359 12.52291
## [25] 12.52230 12.52176 12.52129 12.52089 12.52057 12.52032 12.52015 12.52006
## [33] 12.52005 12.52012 12.52027 12.52050 12.52082 12.52121 12.52167 12.52221
## [41] 12.52281 12.52349 12.52424 12.52506 12.52595 12.52690 12.52792 12.52901
## [49] 12.53016 12.53137 12.53264 12.53398 12.53537 12.53683 12.53834 12.53991
## [57] 12.54154 12.54322 12.54495 12.54674 12.54858 12.55048 12.55242 12.55441
## [65] 12.55645 12.55854 12.56068 12.56286 12.56508 12.56735 12.56966 12.57201
## [73] 12.57441 12.57684 12.57931 12.58182 12.58437 12.58695 12.58966 12.59258
## [81] 12.59572 12.59905 12.60258 12.60628 12.61016 12.61421 12.61840 12.62275
## [89] 12.62723 12.63184 12.63657 12.64141 12.64635 12.65138 12.65650 12.66170
## [97] 12.66696 12.67227 12.67764 12.68305 12.68848 12.69394 12.69942 12.70489
## [105] 12.71037 12.71583 12.72127 12.72667 12.73204 12.73736 12.74262 12.74781
## [113] 12.75293 12.75797 12.76291 12.76775 12.77248 12.77709 12.78157 12.78592
## [121] 12.79060 12.79605 12.80218 12.80892 12.81618 12.82389 12.83198 12.84035
## [129] 12.84894 12.85766 12.86644 12.87519 12.88383 12.89230 12.90050 12.90836
## [137] 12.91581 12.92275 12.92912 12.93484 12.93982 12.94399 12.94837 12.95397
## [145] 12.96065 12.96826 12.97668 12.98577 12.99538 13.00539 13.01565 13.02604
## [153] 13.03640 13.04661 13.05653 13.06603 13.07495 13.08318 13.09057 13.09698
## [161] 13.10229 13.10634 13.10901 13.11123 13.11401 13.11730 13.12103 13.12517
## [169] 13.12966 13.13446 13.13951 13.14477 13.15017 13.15568 13.16124 13.16681
## [177] 13.17233 13.17775 13.18302 13.18810 13.19293 13.19746 13.20164 13.20543
## [185] 13.20877 13.21161 13.21390 13.21559 13.21664 13.21699 13.21659 13.21539
## [193] 13.21334 13.21039 13.20650 13.20160 13.19493 13.18587 13.17465 13.16149
## [201] 13.14661 13.13024 13.11259 13.09389 13.07435 13.05421 13.03369 13.01299
## [209] 12.99236 12.97200 12.95214 12.93300 12.91481 12.89778 12.88214 12.86810
## [217] 12.85590 12.84342 12.82859 12.81162 12.79278 12.77230 12.75042 12.72738
## [225] 12.70342 12.67878 12.65371 12.62845 12.60323 12.57829 12.55389 12.53025
## [233] 12.50763 12.48625 12.46637 12.44822 12.43204 12.41807 12.40519 12.39210
## [241] 12.37884 12.36544 12.35192 12.33833 12.32468 12.31100 12.29733 12.28369
## [249] 12.27012 12.25664 12.24329 12.23009 12.21707 12.20426 12.19169 12.17940
## [257] 12.16740 12.15574 12.14443 12.13396 12.12468 12.11650 12.10928 12.10294
## [265] 12.09733 12.09237 12.08792 12.08389 12.08014 12.07658 12.07309 12.06955
## [273] 12.06585 12.06188 12.05752 12.05267 12.04720 12.04100 12.03396 12.02597
## [281] 12.01811 12.01145 12.00588 12.00129 11.99754 11.99453 11.99214 11.99025
## [289] 11.98874 11.98749 11.98640 11.98533 11.98417 11.98281 11.98112 11.97899
## [297] 11.97630 11.97294 11.96878 11.96371 11.95761 11.95006 11.94085 11.93017
## [305] 11.91823 11.90521 11.89133 11.87676 11.86172 11.84639 11.83098 11.81568
## [313] 11.80069 11.78620 11.77241 11.75952 11.74773 11.73723 11.72822 11.72089
## [321] 11.71545 11.71208 11.70963 11.70683 11.70375 11.70046 11.69701 11.69349
## [329] 11.68995 11.68645 11.68308 11.67989 11.67695 11.67432 11.67207 11.67027
## [337] 11.66899 11.66828 11.66823 11.66888 11.67031 11.67259 11.67578 11.67945
## [345] 11.68315 11.68692 11.69078 11.69477 11.69890 11.70322 11.70776 11.71253
## [353] 11.71758 11.72293 11.72861 11.73465 11.74109 11.74795 11.75525 11.76304
## [361] 11.77134 11.78019 11.78960 11.79961 11.81013 11.82105 11.83237 11.84408
## [369] 11.85618 11.86868 11.88158 11.89486 11.90854 11.92261 11.93707 11.95193
## [377] 11.96717 11.98280 11.99882 12.01523 12.03202 12.04921 12.06677 12.08473
## [385] 12.10307 12.12179 12.14089 12.16038 12.18025 12.20051 12.22114 12.24216
## [393] 12.26355 12.28532 12.30748 12.33001 12.35291 12.37620 12.39986 12.42389
## [401] 12.44830 12.47308 12.49824 12.52377 12.54967 12.57595
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.6, n = 406)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 12.02375 12.01800 12.01233 12.00675 12.00126 11.99585 11.99053 11.98530
## [9] 11.98015 11.97509 11.97011 11.96522 11.96042 11.95570 11.95107 11.94653
## [17] 11.94207 11.93770 11.93341 11.92921 11.92510 11.92107 11.91713 11.91328
## [25] 11.90951 11.90583 11.90224 11.89873 11.89531 11.89197 11.88873 11.88557
## [33] 11.88249 11.87950 11.87660 11.87379 11.87106 11.86842 11.86587 11.86340
## [41] 11.86102 11.85873 11.85652 11.85442 11.85246 11.85063 11.84893 11.84736
## [49] 11.84591 11.84459 11.84339 11.84230 11.84133 11.84048 11.83974 11.83911
## [57] 11.83858 11.83816 11.83785 11.83763 11.83752 11.83750 11.83758 11.83774
## [65] 11.83800 11.83835 11.83878 11.83930 11.83990 11.84057 11.84133 11.84216
## [73] 11.84306 11.84403 11.84507 11.84618 11.84735 11.84858 11.84987 11.85122
## [81] 11.85263 11.85409 11.85559 11.85715 11.85876 11.86042 11.86216 11.86397
## [89] 11.86585 11.86781 11.86985 11.87196 11.87416 11.87642 11.87877 11.88120
## [97] 11.88370 11.88628 11.88895 11.89169 11.89452 11.89743 11.90042 11.90350
## [105] 11.90666 11.90990 11.91323 11.91664 11.92014 11.92373 11.92740 11.93116
## [113] 11.93501 11.93895 11.94298 11.94710 11.95131 11.95561 11.96000 11.96448
## [121] 11.96906 11.97373 11.97849 11.98335 11.98830 11.99335 11.99920 12.00646
## [129] 12.01499 12.02467 12.03535 12.04691 12.05919 12.07208 12.08544 12.09912
## [137] 12.11299 12.12693 12.14079 12.15444 12.16774 12.18055 12.19275 12.20420
## [145] 12.21476 12.22429 12.23267 12.24216 12.25490 12.27055 12.28877 12.30921
## [153] 12.33153 12.35539 12.38046 12.40637 12.43281 12.45941 12.48585 12.51177
## [161] 12.53684 12.56071 12.58305 12.60351 12.62174 12.63741 12.65017 12.65969
## [169] 12.66763 12.67588 12.68438 12.69309 12.70195 12.71093 12.71996 12.72900
## [177] 12.73800 12.74691 12.75568 12.76427 12.77262 12.78069 12.78842 12.79577
## [185] 12.80269 12.80913 12.81504 12.82037 12.82507 12.82910 12.83240 12.83493
## [193] 12.83663 12.83746 12.83736 12.83630 12.83422 12.83106 12.82646 12.82010
## [201] 12.81206 12.80242 12.79127 12.77867 12.76471 12.74946 12.73300 12.71542
## [209] 12.69678 12.67716 12.65665 12.63533 12.61326 12.59053 12.56721 12.54339
## [217] 12.51915 12.49455 12.46968 12.44461 12.41943 12.39421 12.36904 12.34397
## [225] 12.31911 12.29452 12.27028 12.24647 12.22317 12.20046 12.17841 12.15710
## [233] 12.13662 12.11703 12.09841 12.08086 12.06443 12.04921 12.03431 12.01882
## [241] 12.00280 11.98632 11.96944 11.95223 11.93474 11.91705 11.89922 11.88131
## [249] 11.86339 11.84551 11.82775 11.81017 11.79283 11.77579 11.75913 11.74290
## [257] 11.72717 11.71200 11.69745 11.68337 11.66954 11.65595 11.64259 11.62946
## [265] 11.61653 11.60381 11.59128 11.57894 11.56677 11.55476 11.54292 11.53121
## [273] 11.51965 11.50822 11.49690 11.48570 11.47459 11.46358 11.45264 11.44178
## [281] 11.43144 11.42203 11.41347 11.40568 11.39861 11.39216 11.38626 11.38085
## [289] 11.37585 11.37118 11.36676 11.36253 11.35841 11.35433 11.35021 11.34598
## [297] 11.34156 11.33688 11.33186 11.32644 11.32053 11.31388 11.30636 11.29810
## [305] 11.28921 11.27982 11.27004 11.26000 11.24981 11.23960 11.22948 11.21958
## [313] 11.21002 11.20092 11.19239 11.18456 11.17755 11.17148 11.16647 11.16264
## [321] 11.16011 11.15901 11.15851 11.15776 11.15684 11.15580 11.15469 11.15359
## [329] 11.15255 11.15164 11.15091 11.15043 11.15026 11.15046 11.15109 11.15221
## [337] 11.15389 11.15618 11.15915 11.16286 11.16737 11.17274 11.17903 11.18584
## [345] 11.19274 11.19976 11.20693 11.21428 11.22185 11.22966 11.23774 11.24613
## [353] 11.25485 11.26393 11.27342 11.28332 11.29368 11.30453 11.31589 11.32780
## [361] 11.34029 11.35338 11.36711 11.38151 11.39648 11.41189 11.42775 11.44406
## [369] 11.46081 11.47800 11.49564 11.51372 11.53225 11.55121 11.57062 11.59047
## [377] 11.61076 11.63148 11.65265 11.67426 11.69630 11.71879 11.74171 11.76506
## [385] 11.78886 11.81308 11.83775 11.86284 11.88837 11.91434 11.94074 11.96756
## [393] 11.99483 12.02252 12.05064 12.07919 12.10818 12.13759 12.16743 12.19769
## [401] 12.22839 12.25951 12.29106 12.32303 12.35543 12.38825
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")